const Router = require('koa-router');
const {Note} = require('./db');
const router = new Router();


router.post('/note', async (ctx, next) => {
    const {title,content,createTime} = ctx.request.body;

    await new Note({
        title,
        content,
        createTime:createTime
    }).save();
    console.log(title,content)

    ctx.type = 'json'
    ctx.body = {
        status:0,
        msg: '创建成功',
    }
});
router.get('/getList.do', async (ctx, next) => {
    const notes = await Note.find()
    ctx.type = 'json'
    ctx.body = {
        status:0,
        msg: '获取成功',
        data:notes
    }
});



router.put('/put/:id', async (ctx, next) => {
    ctx.type = 'json'
    let {id} = ctx.params;
    let {title,content} = ctx.request.body;

    const note = await Note.findOne({_id: id})
    if(note){
        note.title = title;
        note.content = content;
        note.save();
        ctx.body = {
            status:0,
            msg: '修改成功'
        }
    }else{
        ctx.body = {
            status:1,
            msg: '数据不存在'
        }
    }
});

router.delete('/delete/:id', async (ctx, next) => {
    let {id} = ctx.params;
    await Note.find({_id: id}).remove();

    ctx.type = 'json'
    ctx.body = {
        status:0,
        msg: '删除成功'
    }
});

module.exports = router;